package com.example.administrator.demoa;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class StuAdapter extends RecyclerView.Adapter<StuViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private InOnDelListener delListener;
    private InOnClickListener clickListener;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList,
                      InOnDelListener delListener,InOnClickListener clickListener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.delListener = delListener;
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public StuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.stu_item,viewGroup,false);

        StuViewHolder viewHolder = new StuViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull StuViewHolder stuViewHolder, int i) {
        final Student s = studentArrayList.get(i);
        stuViewHolder.txtNo.setText(s.getNo());
        stuViewHolder.txtName.setText(s.getName());
        stuViewHolder.txtAge.setText(String.valueOf(s.getAge()));

        stuViewHolder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delListener.del(s);
            }
        });

        stuViewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.click(s);
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }
}
